var BaseRegister = app.controller('BaseRegister', function($http, $scope, $rootScope, $location,$cordovaDevice,$cordovaChat,BaseRegisterData) {
	try{
		$rootScope.showf=false;
		$scope.show=false;
		$scope.uname="";
		$scope.pwd="";
		$scope.real_name="";

		$scope.code_value = '获取验证码';
		$rootScope.title = '用户注册';
		$scope.agree = "我同意接受《用户使用协议》";

		$scope.agree_on = true;
		$scope.validate_code='';

		$scope.info={};
		
		$scope.keyPress=function(e){
			if(e.which==13){
				$scope.register();
			}
		}
		$scope.keyPressNext=function(e){
			if(e.which==13||e.which==9){
				$scope.register();
			}
		}
		$scope.dbq=function(){
			if($scope.db_pwd.length==$scope.pwd.length){
				if($scope.db_pwd != $scope.pwd){
					showtoast("两次输入密码不相同，请重新输入");
				}
			}
		}

		var url=SITE_URL+"index.php?app=userapp&callback=JSON_CALLBACK&act=helpcenter";
		$http.jsonp(url).success(function(data) {
			if(data.done){
				$scope.info=data.retval;
			}
		});

		//获取验证码
		$scope.busy=false;
		$scope.get_code=function(){
			if($scope.busy){
				return false;
			}
			$scope.busy=true;
			var url=SITE_URL+"index.php?app=userapp&act=authcode&t="+$scope.uname+"&callback=JSON_CALLBACK";
			$http.jsonp(url).success(function(data) {
				if(data.done){
					//$scope.validate_code=data.retval.code;
					$scope.code_num = 60;
					$scope.code_value = '秒后重发';
					var myTime = setInterval(function() {
						$scope.code_num--;
						$scope.$apply();
						if($scope.code_num==0){

							$scope.busy=false;
							$scope.code_value = '获取验证码';    　
							$scope.code_num =  '';　
							clearInterval(myTime);
							$scope.$apply();
						}
					}, 1000);	
				}else{
					$scope.busy=false;
				}
			}).error(function(){$scope.busy=false;});
		}

		$scope.pai="";

		//验证密码规则
		$scope.checkpwd=function (pass){ 
			var ls = 0; 
			if(pass.match(/([a-z])+/)){  ls++; }  
			if(pass.match(/([0-9])+/)){  ls++; }  
			if(pass.match(/([A-Z])+/)){   ls++; }  
			if(pass.match(/[^a-zA-Z0-9]+/)){ ls++;}  
			return ls; 
		}  

		//确认注册
		$scope.dobusy = false;
		$scope.register=function(){
			if($scope.dobusy){
				return false;
			}

			if($scope.uname == ""){
				showtoast("请输入手机");
				return false;
			}

			var reg = /(^13\d{9}$)|(^14)[5,7]\d{8}$|(^15[0,1,2,3,5,6,7,8,9]\d{8}$)|(^17)[6,7,8]\d{8}$|(^18\d{9}$)/g ;  
			if(!reg.test($scope.uname)){  
				showtoast("请输入正确的手机");
				return false;
			}  

			if($scope.pwd == ""){
				showtoast("请输入用户密码");
				return false;
			}

			if($scope.pwd.length<6){
				showtoast("密码长度至少为6位");
				return false;
			}

			var reg = /^[0-9a-zA-Z]+$/;
			if(!reg.test($scope.pwd)||$scope.checkpwd($scope.pwd)<2){
				showtoast("密码必须为数字与字母混合");
				return false;
			}

			if($scope.db_pwd != $scope.pwd){
				showtoast("两次输入密码不相同，请重新输入");
				return false;
			}

			if($scope.validate_code == ""){
				showtoast("请输入验证码");
				return false;
			}

			$scope.dobusy=true;
			var url=SITE_URL+"index.php?app=userapp&act=register&uname="+$scope.uname+"&pwd="+$scope.pwd+"&callback=JSON_CALLBACK&code="+$scope.validate_code+"&pai="+$scope.pai+"&real_name="+$scope.real_name;
			$http.jsonp(url).success(function(data) {
				if(data.done){
					
					//统计绑定注册用户
					razor('user','BaseRegister',{'uid':data.retval.user_id});
					
					window.localStorage.u=$scope.uname;
					window.localStorage.p=$scope.pwd;
					var requestData={};
					requestData.username=data.retval.user_id;
					requestData.password='123456';
					requestData.nickname=data.retval.real_name;
					$.ajax({
						url: 'https://a1.easemob.com/darven02/dscj/users',
						type: 'POST',
						data: JSON.stringify(requestData),
						headers: {
							'Content-Type': 'application/json'
						},
						success: function (respData) {
							console.log(respData);
						},
						error: function (respData) {
							console.log(respData);
						},
					});
					
					$rootScope.autologin();

					$scope.show=true;

				}else{
					$scope.dobusy=false;
				}
			}).error(function(){$scope.dobusy=false;});
		}	

		$scope.agree_register = function(){
			$scope.agree_on = !$scope.agree_on;
		}
	}
	catch (e) {
		showcatch(e);
	}
});


BaseRegister.BaseRegisterData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u!=undefined&&window.localStorage.u!=null&&window.localStorage.u!=""&&window.localStorage.p!=undefined&&window.localStorage.p!=null&&window.localStorage.p!=""){			
			$rootScope.go("index");
		}else{
			def.resolve(data);
		}
		
		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}